package hos.view;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import hos.po.P_information;
import hos.service.P_informationSerivceImp;

public class LoginView extends JFrame{
	public LoginView(JFrame mainJF) {
		// TODO Auto-generated constructor stub
		JFrame jf = new JFrame("患者注册");
		jf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		jf.setVisible(true);
		//使窗口出现在mainJF的中间位置
		jf.setLocationRelativeTo(mainJF);
		//输入相关信息的panel
		JPanel infoJPanel = new JPanel(new GridLayout(7,2));
		JTextField nameField = new JTextField(20);
		JTextField accountField = new JTextField(20);
		JTextField telField = new JTextField(20);
		JTextField genderField = new JTextField(20);
		JTextField ageField = new JTextField(20);
		JTextField cardField = new JTextField(20);
		JTextField pwdField = new JTextField(20);
		infoJPanel.add(new JLabel("账号："));
		infoJPanel.add(accountField);
		infoJPanel.add(new JLabel("姓名："));
		infoJPanel.add(nameField);
		infoJPanel.add(new JLabel("电话："));
		infoJPanel.add(telField);
		infoJPanel.add(new JLabel("性别："));
		infoJPanel.add(genderField);
		infoJPanel.add(new JLabel("年龄："));
		infoJPanel.add(ageField);
		infoJPanel.add(new JLabel("身份证号："));
		infoJPanel.add(cardField);
		infoJPanel.add(new JLabel("密码："));
		infoJPanel.add(pwdField);
		
		//注册和取消按钮panel
		JPanel loginJPanel = new JPanel();
		JButton loginButton = new JButton("注册");
		JButton exitButton = new JButton("取消");
		loginJPanel.add(loginButton);
		loginJPanel.add(exitButton);
		//退出按钮
		exitButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//销毁窗口（退出）
				jf.dispose();
			}
		});
		
		//注册按钮
		loginButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				P_information newPatient = new P_information();
				newPatient.setP_name(nameField.getText());
				try {
					newPatient.setP_age(Integer.parseInt(ageField.getText()));
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					System.out.println("年龄框无输入");
				}
				newPatient.setP_id(accountField.getText());
				newPatient.setP_idcard(cardField.getText());
				newPatient.setP_tel(telField.getText());
				newPatient.setP_pwd(pwdField.getText());
				//性别要从中文转化为数字
				if(genderField.getText().equals("男")) {
					newPatient.setP_gender(1);
					P_informationSerivceImp p_infoService = new P_informationSerivceImp();
					String returnMessage = p_infoService.P_infoAutoLogin(newPatient);
					System.out.println(newPatient.getP_gender());
					JOptionPane.showMessageDialog(jf, returnMessage,"提示",JOptionPane.INFORMATION_MESSAGE);
					jf.dispose();
				}else if (genderField.getText().equals("女")) {
					newPatient.setP_gender(0);
					P_informationSerivceImp p_infoService = new P_informationSerivceImp();
					String returnMessage = p_infoService.P_infoAutoLogin(newPatient);
					System.out.println(newPatient.getP_gender());
					JOptionPane.showMessageDialog(jf, returnMessage,"提示",JOptionPane.INFORMATION_MESSAGE);
					jf.dispose();
				}
				else {
					//提示性别输入错误的对话框
					JOptionPane.showMessageDialog(jf, "请检查输入信息格式！","警告",JOptionPane.WARNING_MESSAGE);
				}
			}
		});
		//盒式容器
		Box vBox = Box.createVerticalBox();
		vBox.add(infoJPanel);
		vBox.add(loginJPanel);
		
		jf.setContentPane(vBox);
		jf.pack();
	}
	
}
